﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using AjaxControlToolkit.Design;

[assembly: WebResource("TinyMCEExtender.TinyMCEBehavior.js", "text/javascript")]

namespace TinyMCEExtender
{
    /// <summary>
    /// Summary description for TinyMCEExtender
    /// </summary>
    [Designer(typeof (TinyMCEExtenderDesigner))]
    [ClientScriptResource("TinyMCEExtender.TinyMCEBehavior", "TinyMCEExtender.TinyMCEBehavior.js")]
    [TargetControlType(typeof (TextBox))]
    public class TinyMCEExtender : ExtenderControlBase
    {
        #region Properties (2) 

        public string InitParameters { get; set; }

        public string JavaScriptPath { get; set; }

        #endregion Properties 

        #region Methods (1) 

        // Protected Methods (1) 

        protected override void OnLoad(EventArgs e)
        {
            // ** load scripts
            if (Page.Items["tinymce"] == null)
            {
                var Include = new HtmlGenericControl("script");
                Include.Attributes.Add("type", "text/javascript");
                Include.Attributes.Add("src", Page.ResolveUrl(JavaScriptPath));
                Page.Header.Controls.Add(Include);


                //Config MCE
                var Include2 = new HtmlGenericControl("script");
                Include2.Attributes.Add("type", "text/javascript");
                Include2.InnerHtml =
                    "function myCustomCleanup(type, value) {if (type == 'insert_to_editor') {value = value.replace(/&lt;/gi, '<');value = value.replace(/&gt;/gi, '>');}return value;}";
                Page.Header.Controls.Add(Include2);

                //Config MCE
                var Include3 = new HtmlGenericControl("script");
                Include3.Attributes.Add("type", "text/javascript");
                Include3.InnerHtml =
                    "function myCustomSaveContent(element_id, html, body) {html = html.replace(/</gi, '&lt;');html = html.replace(/>/gi, '&gt;');return html;}";
                Page.Header.Controls.Add(Include3);

                //Config MCE
                var Include4 = new HtmlGenericControl("script");
                Include4.Attributes.Add("type", "text/javascript");
                Include4.InnerHtml =
                    "tinyMCE.init({mode : 'specific_textareas', cleanup_callback: 'myCustomCleanup', save_callback: 'myCustomSaveContent', " +
                    InitParameters + " });";
                Page.Header.Controls.Add(Include4);

                Page.Items["tinymce"] = true;

                if (!Page.ClientScript.IsOnSubmitStatementRegistered(GetType(), "tinymcetriggersave"))
                    Page.ClientScript.RegisterOnSubmitStatement(GetType(), "tinymcetriggersave", "tinyMCE.triggerSave(false,true);");
            }

            base.OnLoad(e);
        }

        #endregion Methods 
    }

    public class TinyMCEExtenderDesigner : ExtenderControlBaseDesigner<TinyMCEExtender>
    {
    }
}